﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sauces.Bussinees.Entities.Compras;
using Sauces.Bussinees.Logic.Compras.Service;
using Microsoft.Practices.Unity;
using System.Web.ModelBinding;

namespace Sauces.Web.Compras
{
    public partial class FrmProveedor : System.Web.UI.Page
    {
        [Dependency]
        public IProveedorService ProveedorService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
           var accion = Request.QueryString["action"];
           var IdProveedor = Request.QueryString["IdProveedor"];

           if (accion != null && accion == "new")
           {
               fvProveedor.DefaultMode = FormViewMode.Insert;
           }

           else if (accion != null && IdProveedor != null && accion == "delete")
           {
               var id = Int32.Parse(IdProveedor.ToString());

               ProveedorService.removeProveedor(id);
               Response.Redirect("FrmListaProveedor.aspx");
           }
        }

        public Proveedor getProveedorById([QueryString("IdProveedor")]Int32? id)
        {
            if (id.HasValue)
            {
                return ProveedorService.getFromProveedorById(id.Value);
            }
            return null;
        }

        public void UpdateProveedor(Proveedor proveedor)
        {
            if (ModelState.IsValid)
            {
                ProveedorService.upDateProveedor(proveedor);
                Response.Redirect("FrmListaProveedor.aspx");
            }
        }

        public void AddProveedor(Proveedor proveedor)
        {
            if (ModelState.IsValid)
            {
                ProveedorService.addProveedor(proveedor);
                Response.Redirect("FrmListaProveedor.aspx");
            }
        }


    }
}